# -*- coding: utf-8 -*-
import sys

def renormalize(x):
  i = len(x)-1
  while i > 0:
    if x[i-1] < x[i]: x[i-1] = x[i]
    i -= 1
    
if len(sys.argv) != 3:
  print "specify filename and desired number of hits"
  sys.exit(0)

def buildInterpol(precisao, revocacao):
  normalizedPoints = [0.1*x for x in range(11)]
  i = 0
  #result = []
  my_prec = []
  my_revoc = []
  for i in range(len(revocacao)):
    if revocacao[i] >= normalizedPoints[0]:
      my_prec.append(precisao[i])
      my_revoc.append(normalizedPoints[0])
      normalizedPoints = normalizedPoints[1:]
  return (my_revoc, my_prec)

data = file(sys.argv[1], 'r')
total_a_encontrar = int(sys.argv[2])
#desejados = ['HORATIO']
#desejados = ['MARCELLUS']
#desejados = ['ACT I','ACT II','ACT III','ACT IV','ACT V']
desejados = ['QUEEN GERTRUDE']
# 2 == resultado
# 3 == entrada

precisao = [1.0]
revocacao = [0.0]
lines = data.readlines()[1:]
i = 0
num_encontrados = 0
for line in lines:
  if num_encontrados == total_a_encontrar: break
  tokens = line.split(',')
  tokens = [x.strip()[1:-1] for x in tokens]
  output = tokens[2]
  i += 1  
  if output in desejados:
    num_encontrados += 1
    revocacao.append(num_encontrados/float(total_a_encontrar))
    precisao.append(num_encontrados/float(i))

if total_a_encontrar != num_encontrados:
  precisao.append(0.0)
  revocacao.append(1.0)
 
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

renormalize(precisao)
#ax.plot(revocacao,precisao)
interpolRevocacao, interpolPrecisao = buildInterpol(precisao, revocacao)
#ax.plot(interpolRevocacao, interpolPrecisao)
#print interpolPrecisao
#print precisao
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)

# horatio horacio
# horatio oracio
# horatio oreitio
# marcellus marcel
# marcellus marcelo
# act act
# act acto
# act ato
# queen gertrude gertrudes
# queen gertrude gertrud
# queen gertrude rainha gertrudes
dados = [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.97777777777777775, 0.97391304347826091, 0.97391304347826091, 0.97391304347826091],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.98666666666666669, 0.96703296703296704, 0.96551724137931039, 0.96551724137931039, 0.96551724137931039],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
	 [1.0, 1.0, 1.0, 0.5, 0.5, 0.26666666666666666, 0.26666666666666666, 0.26666666666666666, 0.26666666666666666, 0.0, 0.0],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
	 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
	 [1.0, 0.93617021276595747, 0.93617021276595747, 0.93617021276595747, 0.93617021276595747, 0.93617021276595747, 0.93617021276595747, 0.91935483870967738, 0.91935483870967738, 0.90789473684210531, 0.90789473684210531]]

media = []
for i in range(len(dados)):
  valor = 0.0
  for j in range(len(dados[i])):
    valor += (1/float(11)) * dados[j][i]
  media.append(valor)

print media

ax.plot(interpolRevocacao, media)
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
plt.show()


